<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'shop:notice:shopNotice:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
        <a-popconfirm
          title="是否确认发布公告？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleOnNotice()"
        >
          <a-button type="success" v-auth="'shop:notice:shopNotice:edit'">
            <Icon icon="simple-line-icons:paper-plane" /> {{ t('发布公告') }}
          </a-button>
        </a-popconfirm>
        <a-popconfirm
          title="是否确认取消发布？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleOffNotice()"
        >
          <a-button type="error" v-auth="'shop:notice:shopNotice:edit'">
            <Icon icon="simple-line-icons:action-undo" /> {{ t('取消发布') }}
          </a-button>
        </a-popconfirm>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.ctitle }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsShopNoticeShopNoticeList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { shopNoticeDelete, shopNoticeListData } from '/@/api/shop/notice/shopNotice';
  import { shopNoticeDisable, shopNoticeEnable } from '/@/api/shop/notice/shopNotice';
  import { openNotice, offNotice } from '/@/api/shop/notice/shopNotice';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';

  const { t } = useI18n('shop.notice.shopNotice');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('公告管理管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('公告类型'),
        field: 'ctype',
        component: 'Select',
        componentProps: {
          dictType: 'shop_notice_type',
          allowClear: true,
        },
      },
      {
        label: t('标题'),
        field: 'ctitle',
        component: 'Input',
      },
      {
        label: t('内容简介'),
        field: 'simpleContent',
        component: 'Input',
      },
      {
        label: t('内容详情'),
        field: 'content',
        component: 'Input',
      },
      {
        label: t('APP发布'),
        field: 'bappShow',
        component: 'Select',
        componentProps: {
          dictType: 'sys_yes_no',
          allowClear: true,
        },
      },
      {
        label: t('发布日期'),
        field: 'fbDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'sys_search_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('公告说明'),
        field: 'remarks',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('公告类型'),
      dataIndex: 'ctype',
      key: 'a.ctype',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'shop_notice_type',
    },
    {
      title: t('标题'),
      dataIndex: 'ctitle',
      key: 'a.ctitle',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('内容简介'),
      dataIndex: 'simpleContent',
      key: 'a.simple_content',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('内容详情'),
      dataIndex: 'content',
      key: 'a.content',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('封面图地址'),
      dataIndex: 'picUrl',
      key: 'a.pic_url',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('APP发布'),
      dataIndex: 'bappShow',
      key: 'a.bapp_show',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'sys_yes_no',
    },
    {
      title: t('发布日期'),
      dataIndex: 'fbDate',
      key: 'a.fb_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'sys_search_status',
    },
    {
      title: t('更新时间'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('公告说明'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑公告管理'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'shop:notice:shopNotice:edit',
      },
      {
        icon: 'ant-design:stop-outlined',
        color: 'error',
        title: t('停用公告管理'),
        popConfirm: {
          title: t('是否确认停用公告管理'),
          confirm: handleDisable.bind(this, { id: record.id }),
        },
        auth: 'shop:notice:shopNotice:edit',
        ifShow: () => record.status === '0',
      },
      {
        icon: 'ant-design:check-circle-outlined',
        color: 'success',
        title: t('启用公告管理'),
        popConfirm: {
          title: t('是否确认启用公告管理'),
          confirm: handleEnable.bind(this, { id: record.id }),
        },
        auth: 'shop:notice:shopNotice:edit',
        ifShow: () => record.status === '2',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除公告管理'),
        popConfirm: {
          title: t('是否确认删除公告管理'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'shop:notice:shopNotice:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRowKeys }] = useTable({
    api: shopNoticeListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDisable(record: Recordable) {
    const res = await shopNoticeDisable(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleEnable(record: Recordable) {
    const res = await shopNoticeEnable(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleDelete(record: Recordable) {
    const res = await shopNoticeDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  // 发布公告
  async function handleOnNotice() {
    const ids = getSelectRowKeys();
    if (ids.length) {
      let params = {
        ids: ids.join(','),
      };
      const res = await openNotice(params);
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage(t('请先选择要发布的公告！'));
    }
  }

  // 取消发布
  async function handleOffNotice() {
    const ids = getSelectRowKeys();
    if (ids.length) {
      let params = {
        ids: ids.join(','),
      };
      const res = await offNotice(params);
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage(t('请先选择要取消发布的公告！'));
    }
  }

  function handleSuccess() {
    reload();
  }
</script>
